// pages/routedetails/routedetails.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shiftArray: ['8：00', '9：00', '10：00', '11：00'],
    shiftIndex: -1,
    doomFlag: false,
    animationData: {},
    markers: [{
      iconPath: "../../images/index/icon-orange-map.png",
      id: 0,
      latitude: 23.099994,
      longitude: 113.324520,
      width: 50,
      height: 50
    }, {
      iconPath: "../../images/index/icon-site-map.png",
      id: 0,
      longitude: 113.325074,
      latitude: 23.100225,
      width: 42,
      height: 80
    }, {
      iconPath: "../../images/index/icon-green-map.png",
      id: 0,
      longitude: 113.324243,
      latitude: 23.100314,
      width: 50,
      height: 50
    }],
    polyline: [{
      points: [{
        latitude: 23.099994,
        longitude: 113.324520
      }, {
        longitude: 113.325074,
        latitude: 23.100225
      }, {
        longitude: 113.324243,
        latitude: 23.100314
      }],
      color: '#07c160',
      width: 2,
      dottedLine: false
    }],
    mapHeight: '740rpx',
    coverHeight: '',
    speciallist: [
      { date: '2020-03-31', background: 'rgb(244,245,247)', text: '已售', color: 'red', textBgcolor: '' },
    ],
    calendarFlag: false,
    carDate: '' || '请选择乘车日期',
    carDateChange: [],
    shiftNum: '',
    seatList: [
      { sort: '1', item: [{ name: 'A', flag: '0' }, { name: 'B', flag: '0' }, { name: 'C', flag: '0' }, { name: 'D', flag: '0' }] }
      , { sort: '2', item: [{ name: 'A', flag: '0' }, { name: 'B', flag: '1' }, { name: 'C', flag: '0' }, { name: 'D', flag: '0' }] }
      , { sort: '3', item: [{ name: 'A', flag: '0' }, { name: 'B', flag: '0' }, { name: 'C', flag: '0' }, { name: 'D', flag: '0' }] }
      , { sort: '4', item: [{ name: 'A', flag: '0' }, { name: 'B', flag: '0' }, { name: 'C', flag: '1' }, { name: 'D', flag: '0' }] }
      , { sort: '5', item: [{ name: 'A', flag: '0' }, { name: 'B', flag: '0' }, { name: 'C', flag: '0' }, { name: 'D', flag: '0' }] }
      , { sort: '6', item: [{ name: 'A', flag: '0' }, { name: 'B', flag: '0' }, { name: 'C', flag: '0' }, { name: 'D', flag: '0' }] }
      , { sort: '7', item: [{ name: 'A', flag: '0' }, { name: 'B', flag: '1' }, { name: 'C', flag: '0' }, { name: 'D', flag: '0' }] }
      , { sort: '8', item: [{ name: 'A', flag: '0' }, { name: 'B', flag: '0' }, { name: 'C', flag: '0' }, { name: 'D', flag: '0' }] }
      , { sort: '9', item: [{ name: 'A', flag: '0' }, { name: 'B', flag: '0' }, { name: 'C', flag: '1' }, { name: 'D', flag: '0' }] }
      , { sort: '10', item: [{ name: 'A', flag: '0' }, { name: 'B', flag: '0' }, { name: 'C', flag: '0' }, { name: 'D', flag: '0' }] }], //flag 0:正常可选 1:禁选 2:选中
    itemAcitve: false,
    seatItemFlag: false,
    seatOk: '',
    goTrain: '',
    nextTrain: '',
    goTrainIndex: null,
    nextTrainIndex: null,
    seatIndexSum: null,
    itemIndexSum: null
  },
  seatItemActive: function (e) {
    let _that = this
    let itemflag = e.currentTarget.dataset.itemflag
    let seatidex = e.target.dataset.seatidex
    let itemidex = e.target.dataset.itemidex
    let itemname = e.target.dataset.itemname
    _that.setData({
      seatIndexSum: seatidex,
      itemIndexSum: itemidex
    })
    if (itemflag != 1) {
      var param = {};
      var string = "seatList[" + seatidex + "].item[" + itemidex + "].flag";
      itemflag == 2 ? param[string] = '0' : param[string] = '2';
      _that.setData(param);
      _that.setData({
        seatOk: seatidex + 1 + '排' + itemname + '坐'
      });
    } else {
      return
    }
  },
  //动画 -- 滑入
  slideIn: function () {
    this.animation.translateY(0).step() // 在y轴偏移，然后用step()完成一个动画
    this.setData({
      //动画实例的export方法导出动画数据传递给组件的animation属性
      animationData: this.animation.export()
    })
  },
  seatOk() {
    let _that = this
    _that.setData({
      seatItemFlag: !_that.data.seatItemFlag
    })
  },
  goDoom() {
    wx.navigateTo({
      url: '/pages/orderdetails/orderdetails',
    })
  },
  bindPickerChange: function (e) {
    let _that = this
    _that.setData({
      shiftIndex: e.detail.value,
    })
    _that.setData({
      shiftNum: _that.data.shiftArray[_that.data.shiftIndex]
    })
  },
  goTrainChange: function (e) {
    let _that = this
    wx.navigateTo({
      url: '/pages/point/point?pintFlag=1&goTrain=' + _that.data.goTrain + '&nextTrainIndex=' + _that.data.nextTrainIndex,
    })
  },
  nextTrainChange: function (e) {
    let _that = this
    wx.navigateTo({
      url: '/pages/point/point?pintFlag=0&nextTrain=' + _that.data.nextTrain + '&goTrainIndex=' + _that.data.goTrainIndex
    })
  },
  bindrecordClickedDate: function (e) {
    let _that = this
    _that.setData({
      carDateChange: e.detail
    })
  },
  calendar: function () {
    let _that = this
    _that.setData({
      calendarFlag: !_that.data.calendarFlag
    })
  },
  findData: function (time_stamp) {
    // 获取下一天日期
    var length = 2;
    var time_stamp = time_stamp
    var date_list = [];
    var step = 1; // 跨度
    for (var i = 0; i < length; i++) {
      let getFullYear = new Date(time_stamp + i * 86400000 * step).getFullYear();
      let getMonth = new Date(time_stamp + i * 86400000 * step).getMonth() + 1;
      getMonth = getMonth < 10 ? '0' + getMonth : getMonth
      let getDate = new Date(time_stamp + i * 86400000 * step).getDate();
      getDate = getDate < 10 ? '0' + getDate : getDate
      date_list.push(getFullYear + '-' + getMonth + '-' + getDate);
    }
    return date_list[date_list.length - 1];
  },

  findDate: function (dateList) {
    let count = 0,
      res = [], //保存裁剪位置和数量
      arr = []; //保存结果
    dateList.forEach((ele, i, arr) => {
      let A = new Date(arr[i]),
        B = new Date(arr[i + 1]);
      if (B - A == 86400000) {
        count++
      } else {
        res.push({ i, count });
        count = 0;
      }
    });
    res.forEach(item => {
      let { i, count } = item
      arr.push(dateList.slice(i - count, i + 1))
    })
    return arr
  },

  calendarCli: function () {
    let _that = this
    let detail = _that.data.carDateChange
    let dataItem = ''
    detail.sort(function (a, b) {
      return Date.parse(a.replace(/-/g, "/")) - Date.parse(b.replace(/-/g, "/"));
    });
    let dateSort = _that.findDate(detail);
    if (dateSort.length > 3) {
      for (let i = 0; i < 3; i++) {
        if (dateSort[i].length > 1) {
          dataItem = dataItem + dateSort[i][0] + '~' + dateSort[i][dateSort[i].length - 1] + ','
        } else {
          dataItem = dataItem + dateSort[i][0] + ','
        }
      }
      dataItem = dataItem.replace(/(.*),/, '$1...');
      console.log(dataItem)
    } else {
      for (let i = 0; i < dateSort.length; i++) {
        if (dateSort[i].length > 1) {
          dataItem = dataItem + dateSort[i][0] + '~' + dateSort[i][dateSort[i].length - 1] + ','
        } else {
          dataItem = dataItem + dateSort[i][0] + ','
        }
      }
      dataItem = dataItem.replace(/(.*),/, '$1 ');

    }
    console.log(dataItem)
    _that.setData({
      carDate: dataItem,
      calendarFlag: !_that.data.calendarFlag
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _that = this
    wx.getSystemInfo({
      success: function (res) {
        _that.setData({
          mapHeight: res.windowHeight - (res.windowWidth / 750) * 94 - 300 + "px",
          coverHeight: res.windowHeight - (res.windowWidth / 750) * 94 * 0.92 - 200  + "px"
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _that = this
    let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。
    var currPage = pages[pages.length - 1];
    if (currPage.data.pintFlag === '1') {
      _that.setData({
        goTrain: currPage.data.goTrain,
        goTrainIndex: currPage.data.goTrainIndex,
      })
    } else {
      _that.setData({
        nextTrain: currPage.data.nextTrain,
        nextTrainIndex: currPage.data.nextTrainIndex,
      })
    }
    var that = this;
    // 创建动画实例
    var animation = wx.createAnimation({
      duration: 1000,//动画的持续时间
      timingFunction: 'ease',//动画的效果 默认值是linear->匀速，ease->动画以低速开始，然后加快，在结束前变慢
    })
    this.animation = animation; //将animation变量赋值给当前动画
    var time1 = setTimeout(function () {
      that.slideIn();//调用动画--滑入
      clearTimeout(time1);
      time1 = null;
    }, 100)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})